﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MinimalistFoodStreet.BackEnd.Infrastructure.Dtos.Response
{
    public enum ResponseStatus
    {
        FAIL = -1,
        SUCCESS = 0,
    }
    public class ResponseApiDto
    {
        public int? affectedRows { get; set; }
        public ResponseStatus status { get; set; }
        public string? message { get; set; }
        public object? data { get; set; }
        /// <summary>
        /// 在没有受影响行数时返回成功的方法
        /// </summary>
        /// <param name="msg">成功消息</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static ResponseApiDto Ok(string msg = "成功了", object? data = null)
        {
            return new ResponseApiDto()
            {
                status = ResponseStatus.SUCCESS,
                message = msg,
                data = data
            };
        }
        /// <summary>
        /// 在没有受影响行数时返回失败的方法
        /// </summary>
        /// <param name="msg">失败消息</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static ResponseApiDto Fail(string msg = "失败了", object? data = null)
        {
            return new ResponseApiDto()
            {
                status = ResponseStatus.FAIL,
                message = msg,
                data = data
            };
        }
        /// <summary>
        /// 根据受影响行数返回结果的方法
        /// </summary>
        /// <param name="affectedRows">受影响行数</param>
        /// <param name="successMessage">成功消息</param>
        /// <param name="failMessage">失败消息</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static ResponseApiDto Create(int affectedRows, string successMessage = "成功了", string failMessage = "失败了", object? data = null)
        {
            return new ResponseApiDto()
            {
                affectedRows = affectedRows,
                status = affectedRows > 0 ? ResponseStatus.SUCCESS : ResponseStatus.FAIL,
                message = affectedRows > 0 ? successMessage : failMessage,
                data = data
            };
        }
        /// <summary>
        /// 在没有受影响行数时根据条件返回结果的方法
        /// </summary>
        /// <param name="condition">条件</param>
        /// <param name="successMessage">成功消息</param>
        /// <param name="failMessage">失败消息</param>
        /// <param name="data">数据</param>
        /// <returns></returns>
        public static ResponseApiDto Create(bool condition = true, string successMessage = "成功了", string failMessage = "失败了", object? data = null)
        {
            return new ResponseApiDto()
            {
                status = condition ? ResponseStatus.SUCCESS : ResponseStatus.FAIL,
                message = condition ? successMessage : failMessage,
                data = null
            };
        }
    }
}
